<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class VinServiceProvider extends ServiceProvider
{

    private static $appKey = '73b1ed81b82a5971221e522d803e1dd6';
    private static $appKey_juhe = 'b75c59be1197111c5f3d3fd6bc7d120b';
    private static $vinUrl = 'http://getvin.api.juhe.cn/CarManagerServer/getVINFormat';
    private static $vinJuheUrl = 'http://v.juhe.cn/vinParse/query.php';

// 返回格式：
//{
//"result":{
//"body":{
//"_jtimestamp":"1501563664",
//"_juuid":"2db18f6d4b9b00f45ed726a5ee95e8f9",
//"VIN":"LSGGH59L9DS157185",
//"CARINFO":"上海通用 凯迪拉克 XTS（XTS）三厢 2013款 2.0L 自动 2.0T 手自一体 28T 精英型"
//}
//},
//"reason":"查询成功",
//"error_code":"0"
//}
    /**
     * 到聚合API 获取数据
     * @param $vin
     * @return array
     */
    public static function info( $vin ){

        $res = [
            'status'=>0
        ];
        $url = self::$vinUrl . '?VIN=' . $vin . '&key=' . self::$appKey;
        $data = MyCurlServiceProvider::send( $url );
        $json = json_decode( $data, true );

        $res['status'] = $json['error_code'];
        $res['description'] = $json['reason'];
        if( $json['reason'] == '查询成功' ){
            $res['status'] = 0;
//        if( empty($json['error_code']) ){
            $res['info'] = $json['result']['body']['CARINFO'];
        }else{
            $res['status'] = 1;
        }

        return $res;

    }

    /**
     * 新版领航抓取页面
     * 到领航抓取vin码数据
     * http://car.yhsh.cn/public/ajaxGetBrand?v=LSVHJ133022221761
     * 会跳到中间页面，给出vin码对应的Mi_key
     *
     */
    public static function yhshInfo_middle( $vin ){

        $url = 'http://car.yhsh.cn/public/ajaxGetBrand?v=' . $vin;
        $contents = MyCurlServiceProvider::send( $url );

        require_once ('simple_html_dom.php');
        $html = file_get_html( $url, $contents );
        $list_div = $html->find('div.list-block');
        $list = $list_div[0]->find('a');

        if( !empty( $list ) ){
            foreach( $list as $item ){
                $href = $item->getAttribute('href');
                if( strpos($href, 'Mi_key=') !== false ){
                    list( $pre, $Mi_key ) = explode( 'Mi_key=', $href );
                    $data = self::yhshInfo( $Mi_key, 'Mi_key' );

                    return $data;
                    break;
                }
            }
        }
        return [];
    }

    public static function yhshInfo( $vin, $type='vin' ){

        if( $type == 'vin'){
            $url = 'http://car.yhsh.cn/public/ajaxGetBrand?v=' . $vin;
        }else if( $type == 'Mi_key'){
            $url = 'http://car.yhsh.cn/public/reOil?Mi_key=' . $vin;
        }

        $contents = MyCurlServiceProvider::send( $url );

        require_once ('simple_html_dom.php');
        $html = file_get_html( $url, $contents );
        $items = $html->find('div.item-inner');

        $car_attrs = [
            '主机厂'=>'factory_name',
            '车系'=>'model_name',
            '车型'=>'model_type',
            '发动机描述'=>'engine',
            '年款'=>'year',
            '变速器描述'=>'gearbox',
            '档位数'=>'gearbox_lv',
            '建议使用油品'=>'oil',
            '原厂初装量'=>'oil_regional',
            '循环机更换量'=>'oil_change',
            '产品图片'=>'oil_img_url',
        ];
        $data = [];
        $car_attrs_key = array_keys( $car_attrs );
        foreach( $items as $key=>$item ){
            $title = $item->find('div.item-title',0);
            $title_text = $title->innertext;
            if( !in_array( $title_text, $car_attrs_key ) ){
                continue;
            }
            if( $title_text != '产品图片' ){
                $value = $item->find('div.item-after',0);
                $value_text = $value->innertext;
                $data[$car_attrs[$title_text]] = $value_text;
//                echo $title_text;
//                echo gettype(  $value_text );
            }else{
//                echo $title_text;
//                echo $value;
                $img = $html->find('img',0);
                if( !empty( $img ) ){
                    $value = $img->getAttribute('src');
                    $data[$car_attrs[$title_text]] = $value;
                }else{
                    $data[$car_attrs[$title_text]] = '';
                }

            }
        }
        return $data;
    }

    /**
     * 从龙蟠获取数据
     */
    public static function lopalInfo( $vin ){

        $url = 'http://atf.lopal.cn/index.aspx?vin=' . $vin;
        $cookie = 'ASP.NET_SessionId=43lk2blmfbwkylmguy1hawoo';
        $contents = MyCurlServiceProvider::send( $url, array(), 'get', $cookie );

        require_once ('simple_html_dom.php');
        $html = file_get_html( $url, $contents );
        $items = $html->find('li.biao_ti_cans');

        $car_attrs = [
            '品牌'=>'brand_name',
            '车型'=>'model_name',
//            '车型'=>'model_type',
            '动力配置'=>'engine_displacement',
            '生产日期'=>'year',
            '变速器描述'=>'gearbox',
            '变速箱型号'=>'gearbox_model',
//            '档位数'=>'gearbox_lv',
            '变速箱油'=>'oil',
            '出厂容量'=>'oil_regional',
            '设备换油量'=>'oil_change',
//            '产品图片'=>'oil_img_url',
        ];
        $data = [];
        foreach( $items as $key=>$item ){
            $title = $item->find('span.bsx_bt',0);
            $title_text = $title->innertext;
            $title_text = str_replace( '：', '', $title_text );
            $value = $item->find('span.bsx_nr', 0);
            $value_text = $value->innertext;
            $data[$car_attrs[$title_text]] = $value_text;
        }
        if( !empty($data) ){
            $data['engine'] = $data['engine_displacement'];
            $data['factory_name']=$data['brand_name'];
            $data['model_type']='';
        }

        return $data;

    }

    /**
     * 到聚合API 获取数据,(聚合数据的另一个API, 信息更详细)
     *
     */
    public static function juhe2Info( $vin ){

        $res = [
            'status'=>0
        ];
        $url = self::$vinJuheUrl . '?vin=' . $vin . '&key=' . self::$appKey_juhe;
        $data = MyCurlServiceProvider::send( $url );
        $json = json_decode( $data, true );

        $res['status'] = $json['error_code'];
        $res['description'] = $json['reason'];
        if( $json['reason'] == 'success' && !empty( $json['result']['vehicleList'] ) ){
            $res['status'] = 0;
            $res['info'] = $json['result']['vehicleList'];
        }else{
            $res['status'] = 1;
        }

        return $res;

    }

}
